import {Node as TreeNode , Tree} from "./base-tree.js";

function reConstructBinaryTree(pre , vin){
    if(pre.length === 0){
        return null
    }
    if(pre.length === 1){
        return new TreeNode(pre[0])
    }
    const value = pre[0]
    const index = vin.indexOf(value)
    const vinLeft = vin.slice(0,index)
    const vinRight = vin.slice(index+1)
    const preLeft = pre.slice(1,index+1)
    const preRight = pre.slice(index+1)
    const node = new TreeNode(value)
    node.left = reConstructBinaryTree(preLeft,vinLeft)
    node.right = reConstructBinaryTree(preRight,vinRight)
    return node
}

// 前序 [1,2,4,7,3,5,6,8]
// 中序 [4,7,2,1,5,3,8,6]
const preList = [1,2,4,7,3,5,6,8]
const midList = [4,7,2,1,5,3,8,6]
console.log(reConstructBinaryTree(preList,midList));
const rebuild = reConstructBinaryTree(preList,midList)
 const BaseTree = new Tree()
 let arr = [{'pre':[],'mid':[],'later':[]}]
    arr[0].pre.push(BaseTree.preOrder(rebuild))
    arr[0].mid.push(BaseTree.middleOrder(rebuild))
    arr[0].later.push(BaseTree.laterOrder(rebuild))

 // 根据重建的树节点获取这颗树的基本内容
 console.log(arr);